package com.tudouji.project.common;

import com.flow.service.pojo.BasePojo;
import com.flow.service.pojo.ResultParam;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.bill.domain.Recharge;
import com.tudouji.project.bill.domain.Sellform;
import com.tudouji.project.bill.domain.Transferapply;
import com.tudouji.project.bill.mapper.RechargeMapper;
import com.tudouji.project.bill.mapper.SellformMapper;
import com.tudouji.project.bill.mapper.TransferapplyMapper;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.mapper.CustomerMapper;
import com.tudouji.project.farepay.domain.FarepayApply;
import com.tudouji.project.farepay.mapper.FarepayApplyMapper;
import com.tudouji.project.flow.utils.FlowProcessUtil;
import com.tudouji.project.harvest.domain.Freightorder;
import com.tudouji.project.harvest.domain.Procurementcontract;
import com.tudouji.project.harvest.mapper.FreightorderMapper;
import com.tudouji.project.harvest.mapper.ProcurementcontractMapper;
import com.tudouji.project.pay.domain.Payinfo;
import com.tudouji.project.pay.mapper.PayinfoMapper;
import com.tudouji.project.produce.domain.Materialretapply;
import com.tudouji.project.produce.domain.Pickinglist;
import com.tudouji.project.produce.mapper.MaterialretapplyMapper;
import com.tudouji.project.produce.mapper.PickinglistMapper;
import com.tudouji.project.purchasebill.domain.Purchasebill;
import com.tudouji.project.purchasebill.service.IPurchasebillService;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStoreroomService;
import com.tudouji.project.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通用业务层处理
 *
 * @author tudouji
 * @date 2022-04-21
 */
@Slf4j
@Service
public class CommonServiceImpl
{
    @Autowired
    private IPurchasebillService purchasebillService;
    @Autowired
    private IStoreroomService storeroomService;
    @Autowired
    private FlowProcessUtil flowProcessUtil;
    @Autowired
    private FlowProcessProxy flowProcessProxy;

    @Autowired
    private TransferapplyMapper transferapplyMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PickinglistMapper pickinglistMapper;
    @Autowired
    private MaterialretapplyMapper materialretapplyMapper;
    @Autowired
    private SellformMapper sellformMapper;
    @Autowired
    private RechargeMapper rechargeMapper;
    @Autowired
    private FarepayApplyMapper farepayApplyMapper;
    @Autowired
    private ProcurementcontractMapper procurementcontractMapper;
    @Autowired
    private FreightorderMapper freightorderMapper;
    @Autowired
    private PayinfoMapper payinfoMapper;
    @Autowired
    private FlowProcessProxy processProxy;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public AjaxResult backApproval(Long id, Long nodeId, Long flowId, String content, String fileIds){
        SysUser user= SecurityUtils.getLoginUser().getUser();
        log.info("----id-----------");
        log.info("----------"+(flowId.equals(new Long(26))));
        log.info("-----flowId----"+flowId);
        BasePojo base=null;
        if(flowId.equals(new Long(26))){
            Purchasebill purchasebill = purchasebillService.selectPurchasebillById(id);
            base=purchasebill;
            Storeroom storeroom = storeroomService.selectStoreroomByGuid(purchasebill.getSrguid());
            base.setOrgId(storeroom.getId());
            base.setPlatformId(storeroom.getId());
        }else if(flowId.equals(new Long(28))){
            Transferapply transferapply = transferapplyMapper.selectTransferapplyByNewId(id);
            base=transferapply;
            Storeroom storeroom = storeroomService.selectStoreroomByGuid(transferapply.getTosrguid());
            base.setOrgId(storeroom.getId());
            Storeroom storeroomFrom = storeroomService.selectStoreroomByGuid(transferapply.getFromsrguid());
            base.setOrgId(Long.valueOf(storeroomFrom.getId()));
            base.setPlatformId(storeroom.getId());
            if(transferapply.getTosrguid()!=null){
                Storeroom toStoreroom = storeroomService.selectStoreroomByGuid(transferapply.getTosrguid());
                base.setToOrgId(Long.valueOf(toStoreroom.getId()));
                base.setToPlatformId(toStoreroom.getId());
            }
        }else if(flowId.equals(new Long(29))){
            Customer customer = customerMapper.selectCustomerByNewId(id);
            base=customer;
        }else if(flowId.equals(new Long(30))){
            Pickinglist pickinglist = pickinglistMapper.selectPickinglistByNewId(id);
            base=pickinglist;
        }else if(flowId.equals(new Long(31))){
            Materialretapply apply = materialretapplyMapper.selectMaterialretapplyByNewId(id);
            base = apply;
        }else if(flowId.equals(new Long(34)) || flowId.equals(new Long(35)) ||flowId.equals(new Long(36))){
            Sellform apply = sellformMapper.selectSellformById(id);
            base = apply;
            base.setPlatformId(apply.getStrmId());
        }else if(flowId.equals(new Long(37))){
            Recharge apply = rechargeMapper.selectRechargeByNewId(id);
            base = apply;
        }else if(flowId.equals(new Long(38))){
            FarepayApply apply = farepayApplyMapper.selectFarepayApplyById(String.valueOf(id));
            base = apply;
        }else if(flowId.equals(new Long(39))){
            Procurementcontract apply = procurementcontractMapper.selectProcurementcontractByNewId(id);
            base = apply;
        }else if(flowId.equals(new Long(40))){
            Freightorder apply = freightorderMapper.selectFreightorderByAutoId(id);
            base = apply;
            base.setPlatformId(apply.getStrid());
        }else if(flowId.equals(new Long(41))){
            Payinfo apply = payinfoMapper.selectPayinfoByAutoId(id);
            base = apply;
            base.setPlatformId(apply.getStorermId());
        }else if(flowId.equals(new Long(42))){
            Payinfo apply = payinfoMapper.selectPayinfoByAutoId(id);
            base = apply;
            base.setPlatformId(apply.getStorermId());
        }
        log.info("-----------createBy-------"+base.getCreateBy());
        FlowResult result = flowProcessProxy.toSpecifiedNode(nodeId, base,content, fileIds, user);
        if(result.getMsg().equals(ResultParam.SUCCESS)){
            if(flowId.equals(new Long(26))){
                Purchasebill upBase=new Purchasebill();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                purchasebillService.updatePurchasebill(upBase);
            }

            if(flowId.equals(new Long(28))){
                Transferapply upBase=new Transferapply();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                transferapplyMapper.updateTransferapplyById(upBase);
            }

            if(flowId.equals(new Long(29))){
                Customer upBase=new Customer();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                customerMapper.updatenCustomerNew(upBase);
            }

            if(flowId.equals(new Long(30))){
                Pickinglist upBase=new Pickinglist();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                pickinglistMapper.updatePickinglistNew(upBase);
            }

            if(flowId.equals(new Long(31))){
                Materialretapply upBase=new Materialretapply();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                materialretapplyMapper.updateMaterialretapplyID(upBase);
            }

            if(flowId.equals(new Long(34)) || flowId.equals(new Long(35)) ||flowId.equals(new Long(36))){
                Sellform upBase=new Sellform();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                sellformMapper.updateSellform(upBase);
            }

            if(flowId.equals(new Long(37))){
                Recharge upBase=new Recharge();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                rechargeMapper.updateRecharge(upBase);
            }

            if(flowId.equals(new Long(38))){
                FarepayApply upBase=new FarepayApply();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                farepayApplyMapper.updateFarepayApply(upBase);
            }

            if(flowId.equals(new Long(39))){
                Procurementcontract upBase=new Procurementcontract();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                procurementcontractMapper.updateProcurementcontract(upBase);

            }

            if(flowId.equals(new Long(40))){
                Freightorder upBase=new Freightorder();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                freightorderMapper.updateFreightorder(upBase);

            }
            if(flowId.equals(new Long(41))){
                Payinfo upBase = new Payinfo();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                payinfoMapper.updatePayinfo(upBase);
            }
            if(flowId.equals(new Long(42))){
                Payinfo upBase = new Payinfo();
                upBase.setId(base.getId());
                upBase.setStatus(result.getNextStat());
                payinfoMapper.updatePayinfo(upBase);
            }
            return AjaxResult.success();
        }else{
            return AjaxResult.error(result.getMsg());
        }
    }
}
